package com.bwsoft.admin.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.bwsoft.admin.domain.Advertisement;
import com.bwsoft.admin.domain.Banner;
import com.bwsoft.admin.domain.valueObject.BannerVO;
import com.bwsoft.admin.service.BannerService;
import com.bwsoft.api.util.Constant;
import com.bwsoft.model.Message;
import com.bwsoft.util.UploadFileUtil;

@Service(BannerService.NAME)
public class BannerServiceImpl extends BaseServiceImpl_ybd implements BannerService {
	private static Logger LOG = Logger.getLogger(BannerService.class.getClass());
	
	@Override
	public List<Banner> findData(Integer page, Integer rows,
			String s_bannerName, String s_status) {
		StringBuffer bannerHql = new StringBuffer();
		bannerHql.append("from Banner as bar where 1=1");
		if(s_bannerName != null && !"".equals(s_bannerName.trim())){
			bannerHql.append(" and bar.bannerName like '%"+s_bannerName.trim()+"%'");
		}
		if(s_status != null && !"".equals(s_status.trim())){
			bannerHql.append(" and bar.bannerStatus = '"+s_status.trim()+"'");
		}
		bannerHql.append(" order by bar.bannerOrder");
		List<Banner> bannerData = bannerDao.findPage(bannerHql.toString(), rows, (page - 1) * rows);
		
		return bannerData;
	}
	
	
	@Override
	public List<Banner> findShowBannerData() {
		@SuppressWarnings("unchecked")
		List<Banner> bannerData = bannerDao.findByHql("from Banner as bar where  bar.bannerStatus ='"+1+"' order by bar.bannerOrder asc");
		return bannerData;
	}

	@Override
	public Long getTotalCount(String s_bannerName, String s_status) {
		StringBuffer bannerHql = new StringBuffer();
		bannerHql.append("select count(*) from Banner as bar where 1=1");
		if(s_bannerName != null && !"".equals(s_bannerName.trim())){
			bannerHql.append(" and bar.bannerName like '%"+s_bannerName.trim()+"%'");
		}
		if(s_status != null && !"".equals(s_status.trim())){
			bannerHql.append(" and bar.bannerStatus = '"+s_status.trim()+"'");
		}
		Long count;
		count = (Long)bannerDao.findByHql(bannerHql.toString()).get(0);
		return count;
	}

	@Override
	public Message saveOrUpdateBanner(Banner banner,File annex,String annexName) {
		Message msg = new Message();
		com.bwsoft.admin.domain.File filePojo = new com.bwsoft.admin.domain.File();
		try {
			banner.setAddTime(new Date());
			if(banner.getBannerId() != null && !"".equals(banner.getBannerId())){
				Banner updataBefor = bannerDao.findById(banner.getBannerId());
				banner.setAddTime(updataBefor.getAddTime());
				banner.setBannerSize(updataBefor.getBannerSize());
				banner.setBannerStatus(updataBefor.getBannerStatus());
				banner.setFileid(updataBefor.getFileid());
			}
			//---------附件上传（如果没上传则取原数据库内容保存）--------------
			if(annex != null && !"".equals(annex)){
				Calendar now = Calendar.getInstance();
				String annexFilepath = ServletActionContext.getServletContext().getRealPath(Constant.PATH_PDF_BANNER+"/"+now.get(Calendar.YEAR)+(now.get(Calendar.MONTH) + 1));
				String annexFileName=null ;
				annexFileName=UUID.randomUUID().toString()+annexName.substring(annexName.lastIndexOf('.'), annexName.length());
				UploadFileUtil.uploadOneFile(annexFileName, annex, annexFilepath);
				filePojo.setPath(Constant.PATH_PDF_BANNER+"/"+now.get(Calendar.YEAR)+(now.get(Calendar.MONTH) + 1)+"/"+annexFileName);
				filePojo.setName(annexName);
				filePojo.setSize(String.valueOf(annex.length()));
				filePojo.setAddTime(new Date());
				banner.setFileid(filePojo);
				banner.setBannerStatus(false);
			}
			bannerDao.saveOrUpdate(banner);
			msg.setMsgFlag(true);
			msg.setMsgStr("保存成功！");
		} catch (Exception e) {
			msg.setMsgFlag(false);
			msg.setMsgStr("保存失败：" + e.toString());
			LOG.info(e);
		}
		return msg;
	}

	@Override
	public Banner bannerModifyInfo(Integer id) {
		Banner banner=null;
		try{
			banner = bannerDao.findById(id);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return banner;
	}

	@Override
	public Message remove(String ids) {
		Message msg = new Message();
		try {
			StringBuffer bannerHql = new StringBuffer();
			bannerHql.append("delete from Banner bar where bar.bannerId in ("+ids+")"); 
			bannerDao.executeByHql(bannerHql.toString());
			msg.setMsgFlag(true);
			msg.setMsgStr("保存成功！");
		} catch (Exception e) {
			msg.setMsgFlag(false);
			msg.setMsgStr("保存失败：" + e.toString());
			LOG.info(e);
		}
		return msg;
	}

	@Override
	public Message show(String ids) {
		Message msg = new Message();
		try {
			StringBuffer bannerHql = new StringBuffer();
			bannerHql.append("update Banner bar set bar.bannerStatus=1 where bar.bannerId in ("+ids+")"); 
			bannerDao.executeByHql(bannerHql.toString());
			
			List<Banner> bannerData = bannerDao.findByHql("from Banner as bar where bar.bannerStatus = 1");
			if(bannerData.size()>5){
				bannerDao.executeByHql("update Banner bar set bar.bannerStatus=0 where bar.bannerId in ("+ids+")");
				msg.setMsgFlag(false);
				msg.setMsgStr("Banner图最多显示5个！");
				return msg;
			}
			msg.setMsgFlag(true);
			msg.setMsgStr("保存成功！");
		} catch (Exception e) {
			msg.setMsgFlag(false);
			msg.setMsgStr("保存失败：" + e.toString());
			LOG.info(e);
		}
		return msg;
	}

	@Override
	public Message hide(String ids) {
		Message msg = new Message();
		try {
			StringBuffer bannerHql = new StringBuffer();
			bannerHql.append("update Banner std set std.bannerStatus=0 where std.bannerId in ("+ids+")"); 
			bannerDao.executeByHql(bannerHql.toString());
			msg.setMsgFlag(true);
			msg.setMsgStr("保存成功！");
		} catch (Exception e) {
			msg.setMsgFlag(false);
			msg.setMsgStr("保存失败：" + e.toString());
			LOG.info(e);
		}
		return msg;
	}


	@Override
	public List<BannerVO> getVo(List<Banner> banners) {
		List<BannerVO> bannerVoList = new ArrayList<BannerVO>();
		BannerVO bannerVo = new BannerVO();
		for (Banner banner : banners) {
			bannerVo = new BannerVO();
			bannerVo.setAddTime(banner.getAddTime());
			bannerVo.setBannerId(banner.getBannerId());
			bannerVo.setBannerName(banner.getBannerName());
			bannerVo.setBannerOrder(banner.getBannerOrder());
			bannerVo.setBannerSize(banner.getBannerSize());
			bannerVo.setBannerStatus(banner.getBannerStatus());
			if(banner.getFileid() != null && !"".equals(banner.getFileid())){
				bannerVo.setFileId(banner.getFileid().getId().toString());
				bannerVo.setFileName(banner.getFileid().getName());
				bannerVo.setFilePath(banner.getFileid().getPath());
				bannerVo.setFileSize(banner.getFileid().getSize());
			}
			bannerVoList.add(bannerVo);
		}
		return bannerVoList;
	}




}
